package updown.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import updown.util.DateUtils;

/**
 * <p>Title: ww.kaixin.downupservice.domain </p>
 * <p>Description:updown: - 目标任务信息 </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/11/3
 * @Version: 1.0
 */
@Data
@Builder
@AllArgsConstructor
@Slf4j
public class TaskInfo  extends BaseEntity{
    // 起始、结束、花费事件、任务描述
    @Builder.Default
    private String startTime = DateUtils.getCurDateTime();
    private String endTime;
    private String spendTime;
    @Builder.Default
    private String taskDes = "";
    @Builder.Default
    private Boolean success = Boolean.TRUE;

    private String taskId;

    public void setEndTime(String endTime) {
        this.endTime = endTime;
        this.spendTime = String.valueOf(DateUtils.getSecondsBetweenNoError(startTime, endTime));
        this.endTime = this.endTime.substring(11, 19);
        this.startTime = this.startTime.substring(11, 19);
    }

    public TaskInfo finished() {
        this.endTime = DateUtils.getCurDateTime();
        this.spendTime = String.valueOf(DateUtils.getSecondsBetweenNoError(startTime, endTime));
        this.endTime = this.endTime.substring(11, 19);
        this.startTime = this.startTime.substring(11, 19);
        return this;
    }

    public TaskInfo finishedAddDes(String des) {
        this.endTime = DateUtils.getCurDateTime();
        this.spendTime = String.valueOf(DateUtils.getSecondsBetweenNoError(startTime, endTime));
        this.endTime = this.endTime.substring(11, 19);
        this.startTime = this.startTime.substring(11, 19);
        int len = (this.taskDes + des).length();
        this.taskDes = (this.taskDes + des).substring(0,(len >=256? 256:len));
        return this;
    }

    public TaskInfo finishedUnSuccess() {
        this.success = false;
        this.endTime = DateUtils.getCurDateTime();
        this.spendTime = String.valueOf(DateUtils.getSecondsBetweenNoError(startTime, endTime));
        this.endTime = this.endTime.substring(11, 19);
        this.startTime = this.startTime.substring(11, 19);
        return this;
    }

    public TaskInfo finishedUnSuccessAddDes(String errorMsg) {
        this.success = false;
        this.endTime = DateUtils.getCurDateTime();
        this.spendTime = String.valueOf(DateUtils.getSecondsBetweenNoError(startTime, endTime));
        this.endTime = this.endTime.substring(11, 19);
        this.startTime = this.startTime.substring(11, 19);
        try {
            int len = (this.taskDes + errorMsg).length();
            this.taskDes = (this.taskDes + errorMsg).substring(0,(len >=256? 256:len));
        } catch (Exception e) {
            log.info("finishedUnSuccessAddDes bean error:{}",e);
            log.info("finishedUnSuccessAddDes bean errorMsg:{}",errorMsg);
            this.taskDes = "出错了";
        }
        return this;
    }

    public TaskInfo() {
    }
}
